﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Logistics_Service.DTO;

namespace Logistics_Service.DAO
{
    public class KhachHangDAO
    {
        public KhachHangDTO[] DanhSachKhachHang()
        {
            KhachHangDTO[] arr;
            string query = string.Format("select * from khachhang");
            DataProvider dp = new DataProvider();
            DataTable dt = dp.ExecuteQuery(query);
            int i, n = dt.Rows.Count;
            arr = new KhachHangDTO[n];
            for (i = 0; i < n; i++)
            {
                object kh = GetDataFromDataRow(dt, i);
                arr[i] = (KhachHangDTO)kh;
            }
            return arr;
        }
        public bool ThemKhachHang(string ten, string user, string matkhau, string cmnd, string sdt, string email, DateTime ngaysinh, string diachi )
        {
            email = email.Replace("-", ".");
            DateTime nsinh = DateTime.Parse(ngaysinh.ToString());
            DateTime ngaydky = DateTime.Now;
            diachi = diachi.Replace("-", "/");
            string query = string.Format("insert into khachhang (Ma, Ten, UserName, MatKhau, Email, NgaySinh, CMND, SDT, DiaChi, NgayDangKy) values(N'1',N'{0}',N'{1}',N'{2}',N'{3}','{4}',N'{5}',N'{6}',N'{7}','{8}')", ten, user, matkhau, email, nsinh, cmnd, sdt, diachi, ngaydky);
            DataProvider dp = new DataProvider();
            bool kq = dp.ExecuteNonQuery(query);
            return kq;
        }
        public bool XoaKhachHang(string makh)
        {
            string query = string.Format("update khachhang set daxoa = 0 where ma=N'" + makh +"'");
            bool kq;
            DataProvider dp = new DataProvider();
            kq = dp.ExecuteNonQuery(query);
            return kq;
        }
        public bool CapNhatKhachHang(string ma, string ten, string user, string matkhau, string cmnd, string sdt, string email, DateTime ngaydangky, string xacnhan, string diachi, DateTime ngaysinh)
        {
            DataProvider dp = new DataProvider();
            string query = string.Format("update khachhang set ten = N'{1}', username =N'{2}', matkhau = N'{3}',email = N'{4}', ngaysinh = '{5}', cmnd = N'{6}',sdt = N'{7}', diachi = N'{8}', ngaydangky = '{9}', xacnhan = '{10}' where ma =N'{0}'", ma, ten, user, matkhau, email, ngaysinh, cmnd, sdt, diachi, ngaydangky, xacnhan);
            bool kq;
            kq = dp.ExecuteNonQuery(query);
            return kq;
        }
        protected object GetDataFromDataRow(DataTable dt, int i)
        {
            KhachHangDTO kh = new KhachHangDTO();
            kh.Ma = dt.Rows[i]["Ma"].ToString();
            kh.Ten = dt.Rows[i]["Ten"].ToString();
            kh.UserName = dt.Rows[i]["UserName"].ToString();
            kh.MatKhau = dt.Rows[i]["MatKhau"].ToString();
            kh.CMND = dt.Rows[i]["CMND"].ToString();
            kh.SDT = dt.Rows[i]["SDT"].ToString();
            kh.Email = dt.Rows[i]["Email"].ToString();
            kh.DiaChi = dt.Rows[i]["DiaChi"].ToString();
            kh.NgaySinh = DateTime.Parse(dt.Rows[i]["NgaySinh"].ToString());
            kh.NgayDangKy = DateTime.Parse(dt.Rows[i]["NgayDangKy"].ToString());
            kh.DaXoa = bool.Parse(dt.Rows[i]["DaXoa"].ToString());
            kh.XacNhan = bool.Parse(dt.Rows[i]["XacNhan"].ToString());
            return (object)kh;
        }
    }
}